#include "MainWindow.h"
#include "./ui_MainWindow.h"

#include <QKeyEvent>
#include <QKeySequenceEdit>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){

    ui->setupUi(this);

    // 创建一个动作，这里假设动作是打开文件
    QAction* openAction = new QAction(tr("Open"), this);
    openAction->setShortcut(QKeySequence::Open); // 默认设置为系统标准的打开文件快捷键
    connect(openAction, &QAction::triggered, this, &MainWindow::onOpenFile);

    // 将动作添加到主窗口的菜单栏或工具栏（这里省略了这部分代码）

    // 如果你想自定义一个非标准快捷键，例如 Ctrl+Shift+O
    QShortcut* customShortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_O), this);
    connect(customShortcut, &QShortcut::activated, openAction, &QAction::trigger);

    // 你也可以直接连接到槽函数，而不是动作
    // QShortcut* shortcut = new QShortcut(QKeySequence("Ctrl+Shift+C"), this);
    // connect(shortcut, &QShortcut::activated, this, &MainWindow::customSlot);


    //快捷键拾取
    // 连接按钮点击信号到槽函数
    connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::on_pushButton_clicked);

    // 安装事件过滤器以捕获按键事件
    ui->lineEdit->installEventFilter(this);


    /* 主窗体设置位置与大小 */
    this->setGeometry(0, 0, 800, 480);

    /* 实例化 */
    keySequenceEdit = new QKeySequenceEdit(this);

    /* 设置位置与大小 */
    keySequenceEdit->setGeometry(350, 200, 150, 30);

    /* 信号槽连接 */
    connect(keySequenceEdit,
            SIGNAL(keySequenceChanged(const QKeySequence &)),
            this,
            SLOT(KSEKeySequenceChanged(const QKeySequence &))
            );

}
MainWindow::~MainWindow()
{
    delete ui;
}


bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == ui->lineEdit && event->type() == QEvent::KeyPress) {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        QString keyText1 = QKeySequence(keyEvent->modifiers()).toString();

        std::string str = (keyText1.toStdString());
        QString keyText2 = QKeySequence(keyEvent->key()).toString();
        updateLineEdit(keyText1.toUtf8()+keyText2);
        return true;
    }
    return QMainWindow::eventFilter(obj, event);
}

// bool MainWindow::eventFilter(QObject *obj, QEvent *event)
// {
//     if (obj == ui->lineEdit && event->type() == QEvent::KeyPress) {
//         QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
//         QString keyText = QKeySequence(keyEvent->modifiers() | keyEvent->key()).toString();
//         QString modifiers;

//         if (keyEvent->modifiers() & Qt::ControlModifier) {
//             modifiers += "Ctrl+";
//         }
//         if (keyEvent->modifiers() & Qt::ShiftModifier) {
//             modifiers += "Shift+";
//         }
//         if (keyEvent->modifiers() & Qt::AltModifier) {
//             modifiers += "Alt+";
//         }
//         if (keyEvent->modifiers() & Qt::MetaModifier) {
//             modifiers += "Meta+";
//         }

//         QString fullKeyText = modifiers + keyText;
//         updateLineEdit(fullKeyText);
//         return true;
//     }
//     return QMainWindow::eventFilter(obj, event);
// }


void MainWindow::updateLineEdit(const QString &text)
{
    ui->lineEdit->setText(text);
}

void MainWindow::on_pushButton_clicked()
{
    QString shortcut = ui->lineEdit->text();
    qDebug() << "Shortcut:" << shortcut;
}


void MainWindow::KSEKeySequenceChanged(
    const QKeySequence &keySequence)
{
    /* 判断输入的组合键是否为Ctrl + Q，如果是则退出程序 */
    if(keySequence == QKeySequence(tr("Ctrl+Q"))) {
        /* 结束程序 */
        this->close();
    }else {
        /* 打印出按下的组合键 */
        qDebug()<<"--------->"<<keySequence.toString();
    }
}
